package co.th.bjc.md.controller.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import co.th.bjc.md.dao.Product;
import co.th.bjc.md.dao.ProductImage;
import co.th.bjc.md.dao.Supplier;
import co.th.bjc.md.helper.impl.ImagesHelper;
import co.th.bjc.md.helper.impl.ProductHelper;
import co.th.bjc.md.helper.impl.SupplierHelper;
import co.th.bjc.md.dao.Images;
import co.th.bjc.md.service.interfaces.ProductManageService;

public class ProductManageImpl implements ProductManageService {

	@Override
	public InputStream getImgBlob(Integer id) {
		// TODO Auto-generated method stub
		ImagesHelper prodHelp = new ImagesHelper();
		ProductImage prodImg = null;
		try {
			prodImg = prodHelp.getImageByImgId(id);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return prodImg.getProdImgContent();
	}

	@Override
	public boolean addImg(List<Images> listImg) {
		// TODO Auto-generated method stub
		ImagesHelper prodHelp = new ImagesHelper();
		boolean status = false;
		try {
			Connection con = null;
			List<Images> listResut = prodHelp.addImage(con, listImg); 
			if(listResut.size() > 0){
				status = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}

	@Override
	public List<Product> getAllProduct(String productName, BigDecimal supplierCode) {
		// TODO Auto-generated method stub
		List<Product> list = null;
		try {
			ProductHelper help = new ProductHelper();
			list = help.findProduct(productName, supplierCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Product getEditProduct(BigDecimal id) {
		// TODO Auto-generated method stub
		Product prod = null;
		ProductHelper help = new ProductHelper();
		try {
			prod = help.getEditProduct(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		help
		return prod;
	}

	@Override
	public List<Supplier> getAllSupplier() {
		// TODO Auto-generated method stub
		List<Supplier> supList = null;
		SupplierHelper help = new SupplierHelper();
		try {
			supList = help.getSupplierAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return supList;
	}

	@Override
	public boolean addProduct(Product prod) {
		// TODO Auto-generated method stub
		boolean status = false;
		ProductHelper help = new ProductHelper();
		try {
			help.insert(prod);
			addImg(prod.getImages());
			status = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}

	@Override
	public boolean removeProduct(BigDecimal id) {
		// TODO Auto-generated method stub
		boolean status = false;
		ProductHelper help = new ProductHelper();
		try {
			help.delete(id);
			status = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}

	@Override
	public boolean editProduct(Product prod) {
		// TODO Auto-generated method stub
		boolean status = false;
		ProductHelper help = new ProductHelper();
		try {
			Connection conn = null;
			help.update(conn, prod);
			addImg(prod.getImages());
			status = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}

	@Override
	public boolean removeImage(Integer id) {
		// TODO Auto-generated method stub
		ImagesHelper help = new ImagesHelper();
		Connection conn = null;
		boolean status = false;
		try {
			status = help.delete(conn, id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}
	
}
